﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Chizx.Common
{
    /// <summary>
    /// Cookie工具类
    /// @author Chitry
    /// @email  chitry@126.com
    /// @website http://www.chitry.com
    /// </summary>
    public class CookieHelper
    {
        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="Response"></param>
        /// <param name="cookieName"></param>
        /// <param name="days"></param>
        /// <param name="dats"></param>
        /// <returns></returns>
        public static bool setCookie(HttpResponse Response, string cookieName, int days, params string[] dats)
        {
            HttpCookie cookie = new HttpCookie(cookieName); //定义cookie对象以及名为cookieName的项 
            DateTime dt = DateTime.Now;                  //定义时间对象 
            TimeSpan ts = new TimeSpan(days, 0, 0, 0);           //cookie有效作用时间：单位(天)
            cookie.Expires = dt.Add(ts);                 //添加作用时间 
            for (int i = 0; i < dats.Length; i += 2)
            {
                cookie.Values.Add(dats[i], dats[i + 1]);          //增加属性 
            }
            Response.AppendCookie(cookie);               //确定写入cookie中
            return true;
        }

        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <param name="Request"></param>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string getCookie(HttpRequest Request, string cookieName, string key)
        {
            if (Request.Cookies[cookieName] != null)            //   读取cookie
            {
                string temp = Convert.ToString(Request.Cookies[cookieName].Values[key]);
                //读全部就用Request.Cookies[cookieName].Value) 
                if (temp == "")
                {
                    return null;
                }
                else
                    return temp;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 更新Cookie
        /// </summary>
        /// <param name="Response"></param>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <param name="newvalue"></param>
        public static void updateCookie(HttpResponse Response, string cookieName, string key, string newvalue)
        {
            Response.Cookies[cookieName][key] = newvalue;          // 修改cookie 
            Response.Cookies[cookieName].Expires = DateTime.Now.AddDays(1);
        }

        /// <summary>
        /// 删除指定Cookie下的键值对
        /// </summary>
        /// <param name="Request"></param>
        /// <param name="Response"></param>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        public static void delCookieValue(HttpRequest Request, HttpResponse Response, string cookieName, string key)
        {
            HttpCookie acookie = Request.Cookies[cookieName];
            acookie.Values.Remove(key);                 //  删除cookie下的属性 
            acookie.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(acookie);
        }

        /// <summary>
        /// 删除指定Cookie
        /// </summary>
        /// <param name="Request"></param>
        /// <param name="Response"></param>
        /// <param name="cookieName"></param>
        public static void delCookie(HttpRequest Request, HttpResponse Response, string cookieName)
        {
            HttpCookie aCookie;
            aCookie = new HttpCookie(cookieName);
            aCookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(aCookie);
        }

        /// <summary>
        /// 删除所有Cookie
        /// </summary>
        /// <param name="Request"></param>
        /// <param name="Response"></param>
        public static void delAllCookie(HttpRequest Request, HttpResponse Response)
        {
            HttpCookie aCookie;
            string cookieName;
            int limit = Request.Cookies.Count;
            for (int i = 0; i < limit; i++)
            {
                cookieName = Request.Cookies[i].Name;
                aCookie = new HttpCookie(cookieName);
                aCookie.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(aCookie);
            }
        }
    }
}
